$(function () {
    var postObj = {};
    var apiPath = './api/getPages.php';

    (function (name) {
        //获取分页容器
        var container = $('#pagination-' + name);
        //数据源，最终提供给分页组件的是一个数组
        var sources = function () {
            var result = [];
            var cateNo = getUrlParams().path;
            $.ajax({
                async:false,
                type: "POST",
                url: "./api/getPageNum.php",
                data: { cateNo: cateNo },
                statusCode: {
                    404: function() {
                        alert("page not found");
                    }
                }
            }).done(function( msg ) {
                // alert( "Data Saved: " + msg );
                for (var i = 1; i < msg; i++) {
                    result.push(i);
                }
            }).fail(function() { alert("加载失败"); });
            return result;
        }();

        //定义分页容器参数的相关配置
        var options = {
            dataSource: sources, //数据源
            showGoInput: true, //是否显示跳转指定页输入框，true为显示
            showGoButton: true, //是否显示跳转指定页按钮，true为显示
            pageRange: 1, //页码范围
            pageNumber: window.pageNumber,
            className: window.innerWidth < 680 ? "paginationjs-small" : "paginationjs-big",

            /*
            * 每次翻页时触发的回调
            * @param {array} response 当页数据
            * @param {object} pagination 当页配置信息的对象
            * */
            callback: function (response, pagination) {
                var urlParams = getUrlParams();
                var cateNo = urlParams.path;
                var query = urlParams.query;

                options.pageNumber = pagination.pageNumber;
                window.pageNumber = pagination.pageNumber;

                //定义分页请求的数据
                postObj = {
                    pageNumber: pagination.pageNumber,
                    pageRange: pagination.pageRange,
                    pageSize: pagination.pageSize,
                    mode: query.mode || 'common_list',
                    type: query.type,
                    id: query.id,
                    cateNo: cateNo
                }
                //分页切换时，加载相应的分页数据
                container.prev().load(apiPath + ' .data-container > ul.news-list', postObj);
            }
        };

        //监听窗口变化
        $(window).resize(function () {
            var screeWidth = window.innerWidth;
            //屏幕宽度小于440px时，分页容器大小为小号，否则大号
            // options.className = screeWidth < 680 ? "paginationjs-small" : "paginationjs-big";
            //屏幕宽度小于680px时，分页容器大小为小号，否则大号
            if (screeWidth < 680) {
                $('#pagination-btn .paginationjs').addClass('paginationjs-small').removeClass('paginationjs-big');
            } else {
                $('#pagination-btn .paginationjs').addClass('paginationjs-big').removeClass('paginationjs-small');
            }
        });
        //初始化一个分页容器
        container.pagination(options);

        container.addHook('afterInit', function () {
            $('.data-container').load(apiPath + ' .data-container .news-list', postObj);
        });
    })('btn');
    //切换新闻列表显示模式
    $('.dynamic-container .switch-mode').off(('click')).on('click', function (e) {
        e.preventDefault();
        var oldHref = $(this).attr('href');
        var path = decodeURIComponent(location.href).split('#')[0];
        var urlParams = getUrlParams();
        if (oldHref.indexOf('mode=common_list') != -1) {
            $(this).attr('href', 'mode=media_list')
                .html('<span class="glyphicon glyphicon-picture"></span>图文列表');
        } else {
            $(this).attr('href', 'mode=common_list')
                .html('<span class="glyphicon glyphicon-th-list"></span>普通列表');
        }
        if (!$.isEmptyObject(urlParams.query) && $(urlParams.query).length > 0) {
            var reg = new RegExp($(this).attr('href'),"g");
            var oldParams = urlParams.params.toString().replace(/,/g, '&');
            history.pushState(null, null, path + '#' + urlParams.path +
                '?' + ((oldParams.replace(reg,oldHref) == oldParams) ? (oldParams + '&' +oldHref) : oldParams.replace(reg,oldHref)));
        } else {
            history.pushState(null, null, path + '#' + urlParams.path +
                '?' + oldHref);
        }
        var newHash = decodeURIComponent(location.hash);
        var mode = oldHref.slice(1).split('=').length > 1 ? oldHref.slice(1).split('=')[1] : 'mode=media_list';
        postObj.mode = mode;
        postObj.type = getUrlParams().query.type;
        postObj.id = getUrlParams().query.id;
        postObj.cateNo = newHash.split('?')[0] ? newHash.split('?')[0].slice(1) : '01';
        //加载对应列表的css样式
        $("#load-css").load(apiPath + ' #css-file', postObj);
        //加载对应的列表
        $('.data-container').load(apiPath + ' .data-container .news-list', postObj);

    });
    //加载新闻详情
    $('.data-container').on('click', 'li a', function (e) {
        e.preventDefault();
        location.hash = $(this).attr('href').split('#')[1];
        var url = getUrlParams($(this).attr('href'));
        var path = url.path;
        var postObj = url.query;
        loadInner(path, postObj);
    })
});